//microcommand define -lnnbot-export-i18n
signature("", { hidden: true, slash: false, checkArgCount: true, checkUnknown: true })
action(async ({ session }) => {
  const map = Object.create(null)
  const p = group => {
    for (const [k, v] of Object.entries(group)) {
      if (k[0] === "~") continue
      const [n, y] = k.split(":")
      if (y) map[y] = (n === "group" ? "group:" : "") + (v.$label?.replaceAll("/", ":") || (n === "group" ? y : n.replaceAll("/", "--")))
      if (n === "group") p(v)
    }
  }
  p(ctx.root.config.plugins)

  const tree = ctx.$commander._commandList
    .filter(c => !c.parent)
    .map(function t(c) {
      const a = []
      let p = c.ctx.scope
      while (p?.runtime.plugin) {
        a.push(map[p.key || p.children?.[0]?.key] || p.runtime.plugin.name)
        p = p.parent.scope
      }
      a.pop()
      return {
        name: c.name,
        displayName: c.displayName,
        declaration: c.declaration,
        usage: typeof c._usage === "function" ? c._usage(session) : c._usage,
        permissions: c.permissions,
        options: Object.entries(c._options)
          .filter(([, { hidden }]) => hidden !== true)
          .map(([name, o]) => ({
            name,
            syntax: o.syntax,
            descPath: o.descPath,
            permissions: o.permissions,
          })),
        context: a.filter(Boolean).reverse(),
        children: c.children.map(t),
      }
    })

  await session.onebot?._get("get_group_root_files", { group_id: session.guildId }).then(l => l.files.find(f => f.file_name === "kkkkkk.json")).then(f => f && session.onebot._get("delete_group_file", { group_id: session.guildId, file_id: f.file_id, busid: f.busid })).catch(() => {})

  return h.file(Buffer.from(JSON.stringify({ ...ctx.i18n._data, $_commands: tree })), "application/json", { title: "kkkkkk.json" })
})